package com.manor.common.fragment

import android.content.Context
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import android.widget.FrameLayout
import androidx.annotation.LayoutRes
import androidx.fragment.app.Fragment
import com.manor.common.dialog.WaitingDialog
import com.manor.common.http.HttpClient

/**
 *


 * @date  2020/7/8  14:12
 * @version 1.0
 */
abstract class BaseFragment : Fragment(){

    //是否懒加载
    var isLazyLoad = true

    //是否加载数据（暂时用于第一次加载判断，以后也许会有其他情况）
    private var isNeedLoad = true

    protected var rootView: View? = null

    private var mWaitingDialog: WaitingDialog? = null
    private var handler: Handler? = null


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        rootView = inflater.inflate(getLayoutRes(), container, false)
        return rootView
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
    }

    override fun onResume() {
        super.onResume()
        if (isResumed && isNeedLoad && isLazyLoad) {
            initView()
            //数据已加载，置false，避免每次切换都重新加载数据
            isNeedLoad = false
        }
    }

    override fun onDestroyView() {
        isNeedLoad = true
        dismissWaitingDialog()
        super.onDestroyView()
    }



    @LayoutRes
    protected abstract fun getLayoutRes(): Int

    protected abstract fun initView()


    open fun removeViewFormParent(v: View?) {
        if (v == null) return
        val parent = v.parent
        if (parent is FrameLayout) {
            parent.removeView(v)
        }
    }


    protected open fun closeSoftKeyboard(editText: EditText?) {
        if (context == null || editText == null) return
        val inputMethodManager = context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager?
        inputMethodManager?.hideSoftInputFromWindow(editText.windowToken, 0)
    }

    protected open fun showKeyboard(editText: EditText?) {
        if (context == null || editText == null) return
        val imm = context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager?
        imm?.showSoftInput(editText, InputMethodManager.SHOW_FORCED)
    }


    /**
     * 显示等待对话框
     *
     * @param cancelable
     */
    open fun showWaitingDialog(cancelable: Boolean) {
        if (activity?.isFinishing ==true)
            return
        if (mWaitingDialog == null) {
            mWaitingDialog = WaitingDialog.newDialog(activity)
        }
        if (mWaitingDialog?.isShowing == true) {
            mWaitingDialog?.dismiss()
        }
        mWaitingDialog?.setCancelable(cancelable)
        mWaitingDialog?.show()
        if (handler == null) {
            handler = Handler(Looper.getMainLooper())
        }
        handler?.postDelayed(Runnable { dismissWaitingDialog() }, HttpClient.TIMEOUT.toLong())
    }


    /**
     * 取消等待对话框
     */
    open fun dismissWaitingDialog() {
        if (mWaitingDialog?.isShowing == true) {
            mWaitingDialog?.dismiss()
        }
        handler?.removeCallbacksAndMessages(null)
    }



}